package cn.wolfcode.trip.base.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传工具
 */
public class UploadUtil {
	//图片服务器的文件夹路径
	public static final String IMAGE_PATH = "C:/Users/10783/Documents/OpenSources/tomcat-image/webapps/ROOT";
	public static final String IMAGE_DOMAIN = "http://192.168.14.51:5555/";

	//七牛云域名
	public static final String IMAGE_QINIU_DOMAIN = "http://prqg0dt9l.bkt.clouddn.com/";

	/**
	 * 处理文件上传
	 *
	 * @param file
	 * @return 123.png
	 */
	public static String upload(MultipartFile file) {
		String uuid = UUID.randomUUID().toString();//随机字符串
		String orgFileName = file.getOriginalFilename();//获取文件名称 xxxx.jpg
		String ext = "." + FilenameUtils.getExtension(orgFileName);//   .jpg
		String fileName = uuid + ext; //gdsdsgdsf.jpg
		try {
			File targetFile = new File(IMAGE_PATH, fileName); //D:\OpenSources\tomcat-image\webapps\ROOT\gdsdsgdsf.jpg
			FileUtils.writeByteArrayToFile(targetFile, file.getBytes());//写入文件数据

			return IMAGE_DOMAIN + fileName;//返回文件的http地址

		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 七牛云上传
	 * @param file
	 * @return
	 */
	public static String uploadQiniu(MultipartFile file) {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone2());//机房区域
		//...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);

		//...生成上传凭证，然后准备上传
		String accessKey = "Lg8Bkem0U00Xnd4luspXqR9V3s-OJyxY__emhlcn";
		String secretKey = "gkWMmkSMPvt_nNpB5xsJ8TTPhohhymwWE7w57kSk";
		String bucket = "trip32";//存储空间

		//默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = null;//文件名称
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
			//上传到服务器
			Response response = uploadManager.put(file.getBytes(), key, upToken);
			//解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			//返回文件名
			return IMAGE_QINIU_DOMAIN+putRet.key;
		} catch (QiniuException ex) {
			Response r = ex.response;
			System.err.println(r.toString());
			try {

				System.err.println(r.bodyString());
			} catch (QiniuException ex2) {
				//ignore
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
























